<script setup lang="ts">
  import { ref, watch } from 'vue';
  import { useBooleanStore } from '@/store';
  import { searchAoMaterial } from '@/api/sorting-matching';

  const columns: any[] = [
    {
      title: '序号',
      dataIndex: 'id',
      align: 'center',
    },
    {
      title: '缓存箱编码',
      dataIndex: 'workbinCode',
    },
    {
      title: '工作包编码',
      dataIndex: 'toolCode',
    },
    {
      title: '图号/型号',
      dataIndex: 'itemCode',
    },

    {
      title: '要求数量',
      dataIndex: 'itemQty',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '需要配套数量',
      dataIndex: 'matchQty',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '物料类型',
      dataIndex: 'itemType',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '工作包编码',
      dataIndex: 'packageCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
  ];
  const booleanStore = useBooleanStore();
  const hStyle = ref({
    'background-color': booleanStore.color,
    'border-radius': booleanStore.borderRadius,
  });

  // 创建一个响应式的本地副本
  const localTableData = ref<any[]>([]);
  // 数据库一共有多少条
  const total = ref<number>(0);
  const pageNum = ref<number>(1);
  const pageSize = ref<number>(30);

  // 获取AO物料配套清单
  async function getTableData(item: string) {
    const info = {
      searchParams: {
        msTaskAoId: item,
      },
      pageParameter: {
        rows: pageSize.value,
        page: pageNum.value,
      },
    };
    const res = await searchAoMaterial(info);
    localTableData.value = res?.responseBody?.result || [];
    total.value = res?.responseBody?.pageParameter?.totalCount || 0;
  }

  const demo = ref();

  watch(
    () => booleanStore.row,
    () => {
      if (booleanStore.num7 === 1) {
        console.log(booleanStore.row);
        demo.value = booleanStore.row;
        getTableData(booleanStore.row);
      }
    }
  );

  function change(e: any) {
    pageNum.value = e;
  }
  function changeSize(e: any) {
    pageSize.value = e;
  }
  // 监听页面变化
  watch([pageNum, pageSize], () => {
    getTableData(demo.value);
  });
</script>

<template>
  <a-card :header-style="hStyle" :bordered="false">
    <template #title>
      <div class="font">AO物料配套清单</div>
    </template>

    <a-table
      :stripe="true"
      class="top-table"
      :pagination="false"
      :columns="columns"
      :data="localTableData"
      :scroll="{ y: 400 }"
      style="height: 250px"
    >
      <template #id="{ record }">
        {{ localTableData.indexOf(record) + 1 }}
      </template>
    </a-table>
    <div class="page-position">
      <a-pagination
        :total="total"
        :current="pageNum"
        :page-size="pageSize"
        show-total
        show-jumper
        show-page-size
        :page-size-options="[30, 50, 100, 500]"
        @change="change"
        @update:page-size="changeSize"
      />
    </div>
  </a-card>
</template>

<style scoped>
  .top-table {
    white-space: nowrap;
  }
  .page-position {
    margin-top: 20px;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .font {
    font-size: 20px;
    color: black;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI',
      Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue',
      sans-serif;
  }
</style>
